﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace EpicBall.ScreenSystem
{
    public class MenuScreen : GameScreen
    {
        protected List<MenuButton> menuBtns;          //Keeps track of all menu buttons
        public Vector2 mousePos;                   //Current position of mouse
        protected Texture2D cursor;

        public MenuScreen() : this(0, 0) { }
        public MenuScreen(int screenWidth, int screenHeight) : base(screenWidth, screenHeight)
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.75);
            TransitionOffTime = TimeSpan.FromSeconds(0.75);
            menuBtns = new List<MenuButton>();
        }

        /// <summary>
        /// Add a new menubutton to the menu
        /// </summary>
        /// <param name="menuBtn"></param>
        /// 
        public void AddMenuButton(MenuButton menuBtn)
        {
            menuBtns.Add(menuBtn);
        }

        public override void LoadContent()
        {
            base.LoadContent();
            mousePos = Vector2.Zero;
            cursor = GameStack.Content.Load<Texture2D>("MainMenu/cursor");      //Load the custom defined cursor
        }

        public override void Update(GameTime gameTime)
        {
            UpdateMenuBtns(gameTime);
            HandleMouse();
            base.Update(gameTime);
        }

        public void UpdateMenuBtns(GameTime gameTime)
        {
            foreach (MenuButton m in menuBtns)
            {
                m.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            GameStack.SpriteBatch.Begin();
            //Draw all the menu buttons onto the screen
            foreach (MenuButton m in menuBtns)
            {
                m.Draw(gameTime);
            }
            GameStack.SpriteBatch.Draw(cursor, mousePos, Color.White);
            GameStack.SpriteBatch.End();
            base.Draw(gameTime);
        }

        /// <summary>
        /// Performs mousechecks to see if a menu button is clicked
        /// </summary>
        protected void HandleMouse()
        {
            MouseState mouseState = Mouse.GetState();
            mousePos.X = MathHelper.Clamp(mouseState.X, 0, GameStack.Dimensions.X);
            mousePos.Y = MathHelper.Clamp(mouseState.Y, 0, GameStack.Dimensions.Y);
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                //Do mouse checks here
                for (int i = 0; i < menuBtns.Count; i++)
                {
                    if (menuBtns[i].isHover)
                    {
                        menuBtns[i].ButtonClicked();
                    }
                }
            }
        }
    }
}
